﻿var selState;
var selStatePopulated = false;
var serviceNotAvailable = false;
var timer;
var updateInterval = 10000;
var updateInfo;

function Page_Load(sender, e)
{
    $addHandler($get("lnkUpdate"), "click", lnkUpdate_Click);
    
    selState = $find('selState');
    
    System.Gadget.onDock = Gadget_onDock;
    System.Gadget.onUndock = Gadget_onUndock;
    System.Gadget.Flyout.onHide = Flyout_onHide;
    System.Gadget.settingsUI = "settings.html";
    
    DocOrUndock();
    
    CheckForUpdate();
    
    //Turns out that there is no consistent way to check for 
    //network connectivity using the v1 gadget APIs.  
    //Therefore, we do it manually by trying to contact the 
    //server every ten seconds.  We could implement a lighterweight 
    //"Ping()" WebMethod in our service to facilitate this, but 
    //since checking for an update is something we need to do and it 
    //is rather light anwyay, we'll just constantly do an update 
    //check when the gadget is visible (i.e. the user can see it).  
    //If the gadget is not visible, then we back off completely.
    
    //TODO: Implement a better network detection scheme using:
    //http://msdn2.microsoft.com/en-us/library/aa370795.aspx
    //http://blogs.msdn.com/knom/archive/2007/03/26/vista-network-list-manager.aspx
    System.Gadget.visibilityChanged = CheckVisibility;
    timer = setInterval(CheckForUpdate, updateInterval);    
}

function lnkUpdate_Click()
{
    System.Gadget.close();
}

function DocOrUndock()
{
    if (System.Gadget.docked) 
    {
        Gadget_onDock();
    }
    else 
    {
        Gadget_onUndock();
    }  
}

function CheckVisibility()
{
    //Turn timer off.  This causes the gadget to 
    //completely back off when it is not visible.
    clearInterval(timer);
    
    //If gadget is visible, then turn the timer back on and
    //check the server
    if(System.Gadget.visible)
    {
        //CheckForUpdate has builtin logic to display
        //"Service Not Available" message in the
        //error callback if we can't call the service
        CheckForUpdate();
        timer = window.setInterval(CheckForUpdate, updateInterval);
    }
}

function CheckForUpdate()
{
    var version = "1.0.0.5";    
    version = System.Gadget.version;
    
    //Get data from service to populate dropdown
    PerDiemService.CheckForUpdate(version, 
                                    OnCheckForUpdateComplete, 
                                    OnError);
}

function OnCheckForUpdateComplete(result)
{
    updateInfo = result;
    
    serviceNotAvailable = false;
    
    if (updateInfo.IsUpdateAvailable)
    {
        //suspend timer since we're in a forced update situation
        clearInterval(timer);
        ShowUpdateUI();
    }
    else
    {   
        if (!selStatePopulated)
        {
            PopulateSelState();
        }
        else
        {
            DocOrUndock();
        }
    }
}
        
function PopulateSelState()
{
    PerDiemService.GetStates(OnGetStatesComplete, OnError);
}

function OnGetStatesComplete(states)
{    
    selState.set_data(states);
    selStatePopulated = true;
    
    ShowDockedUI();
}


function OnError(error)
{
    System.Gadget.Flyout.show = false;
    
    ShowServiceNotAvailableUI();
    
    serviceNotAvailable = true;
}

function selState_SelectionChanged(sender, e)
{
    var state = selState.get_selectedValue();
    
    if (state != 'Select a state...')
    {
        System.Gadget.Settings.write('state', state);   
        
        System.Gadget.Flyout.file = 'flyout.html';
        System.Gadget.Flyout.show = true;
    }
}

function Flyout_onHide()
{
    selState.set_selectedValue('Select a state...');
}

function Gadget_onDock()
{
    document.body.style.width = "130px";
    document.body.style.height = "100px";
        
    ShowDockedUI(); 
}

function Gadget_onUndock()
{
	// Change size in rev two with the map integration
	// For now....just show same as docked UI
	ShowDockedUI(); 
	
    //document.body.style.width = "300px";
    //document.body.style.height = "200px";   
        
    //ShowUndockedUI();        
}

function ShowDockedUI()
{
    if (serviceNotAvailable)
    {        
        ShowServiceNotAvailableUI();
    }
    else
    {        
    	if (selStatePopulated)
    	{
    		// Hide the loading div and show the dropdown
		    divGettingData.style.display = "none";
			divMain.style.display = "block";
    	}
    	
        divSericeNotAvailable.style.display = "none";
        divUpdate.style.display = "none";
        divUndocked.style.display = "none";
        
        divDocked.style.display = "block";
     }   
}

function ShowUndockedUI()
{    
    if (serviceNotAvailable)
    {
        ShowServiceNotAvailableUI();
    }
    else
    {        
        divGettingData.style.display = "none";
        divSericeNotAvailable.style.display = "none";
        divUpdate.style.display = "none";
        divDocked.style.display = "none";        
        
    	divMain.style.display = "block";
        divUndocked.style.display = "block";
    }
}

function ShowServiceNotAvailableUI()
{
    divGettingData.style.display = "none";
    divMain.style.display = "none";
    divUpdate.style.display = "none";
        
    divSericeNotAvailable.style.display = "block";  
}

function ShowUpdateUI()
{
        //currently, we force an upgrade
        //by design because we don't want to have
        // to support multiple versions of the gadget
        System.Gadget.Flyout.show = false;
        
        lnkUpdate.href = updateInfo.UpgradeUrl;
        
        divGettingData.style.display = "none";        
        divSericeNotAvailable.style.display = "none";
        divMain.style.display = "none";
        
        divUpdate.style.display = "block";
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();